<?php


namespace app\admin\controller;

use app\admin\model\StuffModel;
use cmf\controller\AdminBaseController;
use think\Db;

class StuffController extends AdminBaseController
{

    public function initialize()
    {
        parent::initialize();
    }

    /*材料库*/
    public function index()
    {
        $pagenum = $this->request->param('page');

        $this->cate_name();
        $stuff_name = $this->request->param("stuff_name");
        $cate = $this->request->param("cate_name");

        $result = Db::name('stuff')->alias('s')->field('s.*,c.cate_name')
            ->join('dj_cate c', 'c.id=s.cate_id')
            ->where('s.stuff_name', 'like', '%' . $stuff_name . '%')
            ->where('cate_name', 'like', '%' . $cate . '%')
            ->order("list_order","ASC")
            ->paginate();

        //echo Db::name('stuff')->getLastSql();

        $page = $result->render();
        $this->assign('results', $result);
        $this->assign('cate_name', $cate);
        $this->assign("page", $page);
        return $this->fetch();
    }

    /*石材名称*/
    public function cate_name()
    {
        $cate = Db::name('cate')
            ->where('type', 6)
            ->where('if_show',1)
            ->select();
        $this->assign('cate', $cate);
    }

    /*材料库--添加跳转*/
    public function add()
    {
        $cate_name = Db::name('cate')
            ->where('type', 6)
            ->where('if_show',1)
            ->where('is_delete','0')
            ->select();

        $this->assign('cate_name', $cate_name);
        return $this->fetch();
    }
    // 排序
    public function listOrder()
    {
        $stuffModel = new StuffModel();
        parent::listOrders($stuffModel);
        $this->success("排序更新成功！");
    }
    /*材料库--添加保存*/
    public function add_post()
    {
        $data = $this->request->param();
        $data['add_time'] = time();
        $data['add_user'] = cmf_get_current_admin_id();
        $data['is_delete'] = 0;

        Db::name('stuff')->insert($data);

        //echo Db::name('stuff')->getLastSql();
        $this->success("添加成功", 'stuff/index');
    }


    /*材料库--编辑跳转*/
    public function edit()
    {
        $this->cate_name();

        $id = $this->request->param('id');
        $pagenum = $this->request->param('page');

        $result = Db::name('stuff')->where('id', $id)->find();

        //echo Db::name('stuff')->getLastSql();
        $this->assign($result);
        $this->assign("result", $result);
        $this->assign("pagenum", $pagenum);

        return $this->fetch();
    }

    /*材料库--编辑保存*/
    public function edit_post()
    {
        $data = $this->request->param();
        $pagenum = $data['pagenum'];

        $newdata = array();
        $newdata['id'] = $data['id'];
        $newdata['cate_id'] = $data['cate_id'];
        $newdata['stuff_name'] = $data['stuff_name'];
        $newdata['stuff_money'] = $data['stuff_money'];
        $newdata['stuff_unit'] = $data['stuff_unit'];
        $newdata['stuff_image'] = $data['stuff_image'];
        $newdata['list_order'] = $data['list_order'];

        Db::name('stuff')->update($newdata);
        $this->success("保存成功", url('Stuff/index').'?page='.$pagenum);
    }

    /*材料库--删除*/
    public function delete()
    {
        $id = $this->request->param();
        $cate =  Db::name('stuff')->where('id',$id['id'])->find();
        if(empty($cate)){
            $this->error("没有找到该数据");
        }

        // 判断该材料下有没有产品
        $icate = Db::name("product_info")->where('icate', $id['id'])->find();
        if(!empty($icate)){
            $this->error("请先删除该材料下的产品");
        }
        Db::name('stuff')->delete($id);

        $this->success('删除成功', 'Stuff/index');
    }
}